package com.aiju.cash.ui.enter;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.aiju.base.activity.BaseTitleActivity;
import com.aiju.cash.R;
import com.aiju.cash.ui.enter.util.EnterHelper;
import com.aiju.cash.util.ImageDialogUtil;
import com.aiju.forexpand.datepicker.DatePicker;
import com.aiju.utils.DateUtil;
import com.aiju.utils.T;
import com.aiju.widgit.layout.SettingItemEditView;
import com.aiju.widgit.layout.SettingItemTextView;

import java.io.File;

/**
 * @author: 佐为
 * @date: 2017-05-26 09:47
 * @version: V1.0
 * @Description: 营业执照上传
 */
public class PermitImageActivity extends BaseTitleActivity {

    private ImageView imageView;

    private SettingItemEditView number, name;
    private SettingItemTextView timeLong, time;
    private File logoFile;

    private String licence;//图片id
    private String licence_code, licence_name, business_certificate_expires;//营业执照code,注册号，名称

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_permint);
        setCenterTitle("营业执照");
        setRightTitle("确认上传");

        licence = getIntent().getStringExtra("image_id");
        licence_name = getIntent().getStringExtra("licence_name");
        licence_code = getIntent().getStringExtra("licence_code");
        business_certificate_expires = getIntent().getStringExtra("business_certificate_expires");

        mAiJuTitleBarView.setRightOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                licence_code = number.getRightString();
                licence_name = name.getRightString();
                if (logoFile == null && TextUtils.isEmpty(licence)) {
                    T.ToastShort("请上传营业执照");
                    return;
                }
                if (TextUtils.isEmpty(business_certificate_expires)) {
                    T.ToastShort("请选择营业执照有效期");
                    return;
                }
                if (TextUtils.isEmpty(licence_code)) {
                    T.ToastShort("请填写注册号");
                    return;
                }
                if (TextUtils.isEmpty(licence_name)) {
                    T.ToastShort("请填写字号名称/名称");
                    return;
                }
                Intent intent = new Intent();
                intent.putExtra("licence_name", licence_name);
                intent.putExtra("licence_code", licence_code);
                intent.putExtra("business_certificate_expires", business_certificate_expires);
                if (logoFile != null) {
                    EnterHelper.uploadImageFile(PermitImageActivity.this, mDialogHelper, intent, logoFile);
                } else {
                    intent.putExtra("data", licence);
                    setResult(200, intent);
                    finish();
                }
            }
        });
        imageView = (ImageView) findViewById(R.id.iv_enter_add);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ImageDialogUtil.showSelectPicDialog(PermitImageActivity.this);
            }
        });
        EnterHelper.displayImage(getIntent(), mDialogHelper, imageView);
        number = (SettingItemEditView) findViewById(R.id.set_permit_number);
        number.getRightView().setHint("请严格按照营业执照填写，最多30位数字");
        number.getRightView().setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        number.setEditable(true);

        name = (SettingItemEditView) findViewById(R.id.set_permit_name);
        name.getRightView().setHint("请填写营业执照上的名称");
        name.setEditable(true);

        timeLong = (SettingItemTextView) findViewById(R.id.set_permit_long);
        Drawable drawable = getResources().getDrawable(R.drawable.selector_setting);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        TextView rightView = timeLong.getRightView();
        rightView.setCompoundDrawables(null, null, drawable, null);
        timeLong.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                v.setSelected(!v.isSelected());
                if (v.isSelected()) {
                    time.setVisibility(View.GONE);
                    business_certificate_expires = "长期";
                } else {
                    time.setVisibility(View.VISIBLE);
                    business_certificate_expires = time.getRightString();
                }
            }
        });
        time = (SettingItemTextView) findViewById(R.id.set_permit_time);
        time.setRightText(DateUtil.getCurrentDate(DateUtil.dateFormatYMD));
        time.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showBirthday(time.getRightView(), "选择时间");
            }
        });

        if (!TextUtils.isEmpty(licence_code)) {
            number.setRightText(licence_code);
        }
        if (!TextUtils.isEmpty(licence_name)) {
            name.setRightText(licence_name);
        }
        if (!TextUtils.isEmpty(business_certificate_expires)) {
            if (business_certificate_expires.equals("长期")) {
                timeLong.setSelected(true);
                time.setVisibility(View.GONE);
            } else {
                time.setRightText(business_certificate_expires);
            }
        }
    }

    /**
     * 选择日期
     */
    private void showBirthday(TextView textView, String title) {
        DatePicker dataPicker = new DatePicker(this);
        dataPicker.selectDateDialog(textView, DateUtil.getCurrentDate(DateUtil.dateFormatYMD));
        dataPicker.getTitle().setText(title);
        dataPicker.setChangeDate(new DatePicker.OnChangeDate() {
            @Override
            public void onChangeDate(String date) {
                business_certificate_expires = date;
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        logoFile = EnterHelper.onActivityResult(imageView, requestCode, resultCode, data);
    }
}
